package com.cary.demo

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.accessibility.AccessibilityEvent
import android.graphics.Path
import android.util.Log.d
import android.util.Log.e
import kotlinx.coroutines.delay

// 新建 AccessibilityService 类
 class SwipeAccessibilityService : AccessibilityService() {
    private lateinit var receiver: BroadcastReceiver
    var count = 0
    companion object {
        private const val TAG = "CCCC SwipeAccessibilityService"
        const val ACTION_PERFORM_SWIPE = "com.cary.demo.PERFORM_SWIPE"
        const val ACTION_SWIPE_COMPLETED = "com.cary.demo.SWIPE_COMPLETED"
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        // 处理无障碍事件
    }

    override fun onInterrupt() {
        // 中断处理
    }
    override fun onServiceConnected() {
        super.onServiceConnected()
        d(TAG, "CCCC 服务已连接")

        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                d(TAG, "CCCC onReceive: action=${intent?.action}")
                if (intent?.action == ACTION_PERFORM_SWIPE) {
                    d(TAG, "收到上划指令，执行上划")
                    performSwipeUp()
                } else {
                    d(TAG, "收到其他广播: ${intent?.action}")
                }
            }
        }

        val filter = IntentFilter(ACTION_PERFORM_SWIPE)
        registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
        d(TAG, "CCCC 广播接收器已注册")
    }

    override fun onDestroy() {
        super.onDestroy()
        d(TAG, "CCCC onDestroy")
        unregisterReceiver(receiver)
    }
    fun performSwipeUp() {
        count++
        val displayMetrics = resources.displayMetrics
        val screenHeight = displayMetrics.heightPixels
        val screenWidth = displayMetrics.widthPixels

        val gestureBuilder = GestureDescription.Builder()
        val path = Path()
        val startX = screenWidth / 2
        val startY = (screenHeight * 0.8).toInt()
        val endY = (screenHeight * 0.5).toInt()
        val randomStartX = (10..startX).random()
        val randomLineX = (20..startX+20).random()
        val randomStartY = (70..startY).random()
        val randomEndY = (10..endY).random()
        d(TAG, "performSwipeUp: startX=$startX, startY=$startY, endY=$endY")
        d(TAG, "performSwipeUp: randomStartX=$randomStartX, randomStartY=$randomStartY, randomEndY=$randomEndY")
        path.moveTo(startX.toFloat(), startY.toFloat())
        path.lineTo(randomLineX.toFloat(), endY.toFloat())

        gestureBuilder.addStroke(
            GestureDescription.StrokeDescription(
                path,
                0,
                500 // 持续时间500ms
            )
        )

        // 添加回调来确认是否执行成功
        dispatchGesture(gestureBuilder.build(), object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription?) {
                super.onCompleted(gestureDescription)
                d(TAG, "上划手势执行成功")
                // 发送滑动完成广播
                val completeIntent = Intent(ACTION_SWIPE_COMPLETED)
                completeIntent.setPackage(packageName)
                sendBroadcast(completeIntent)

            }

            override fun onCancelled(gestureDescription: GestureDescription?) {
                super.onCancelled(gestureDescription)
                e(TAG, "上划手势执行失败")
                // 可选：滑动失败时也发送广播
                val failIntent = Intent(ACTION_SWIPE_COMPLETED)
                failIntent.setPackage(packageName)
                sendBroadcast(failIntent)
            }
        }, null)
    }
}
